package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.EducationalDegreeMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.EducationalDegree;
import edu.hubu.talentmanagementsystem.service.IEducationalDegreeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class EducationalDegreeServiceImpl extends ServiceImpl<EducationalDegreeMapper, EducationalDegree> implements IEducationalDegreeService {

    private final MapperContext context;

    public EducationalDegreeServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getEducationalDegreeMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<EducationalDegree> insert(@EncodeParam EducationalDegree record) {
        int insert = context.getEducationalDegreeMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getEducationalDegreeMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<EducationalDegree> selectById(Integer id) {
        QueryWrapper<EducationalDegree> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<EducationalDegree> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(EducationalDegree::getEducationalDegreeId, id);
        return Optional.of(context.getEducationalDegreeMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<EducationalDegree> selectAll(String order) {
        QueryWrapper<EducationalDegree> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<EducationalDegree> select = getColumnWrapper(wrapper.lambda());
            return context.getEducationalDegreeMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<EducationalDegree> select = getColumnWrapper(wrapper.lambda());
        return context.getEducationalDegreeMapper().selectList(select);
    }

    @Override
    public Page<EducationalDegree> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<EducationalDegree> wrapper = new QueryWrapper<>();
        Page<EducationalDegree> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<EducationalDegree> select = getColumnWrapper(wrapper.lambda());
            return context.getEducationalDegreeMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<EducationalDegree> select = getColumnWrapper(wrapper.lambda());
        return context.getEducationalDegreeMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<EducationalDegree> getColumnWrapper(LambdaQueryWrapper<EducationalDegree> wrapper) {
        return wrapper.select(
                EducationalDegree::getEducationalDegreeId
                    ,
                EducationalDegree::getEducationalDegreeName
        );
    }

    @Override
    public Page<EducationalDegree> selectLikePaged( Integer currentPage,
             Integer pageSize, EducationalDegree record, String order) {
        Page<EducationalDegree> page = new Page<>(currentPage, pageSize);
        QueryWrapper<EducationalDegree> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getEducationalDegreeMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getEducationalDegreeMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<EducationalDegree> getQueryWrapper(QueryWrapper<EducationalDegree>wrapper, EducationalDegree record) {
        LambdaQueryWrapper<EducationalDegree> lambda = wrapper.lambda();
        if (record.getEducationalDegreeId() != null) {
            lambda = lambda.eq(EducationalDegree::getEducationalDegreeId,record.getEducationalDegreeId());
        }
        if (record.getEducationalDegreeName() != null) {
            lambda = lambda.like(EducationalDegree::getEducationalDegreeName,record.getEducationalDegreeName ());
        }
        return lambda;
    }

    public int count(EducationalDegree record) {
        return context.getEducationalDegreeMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<EducationalDegree> updateById(Integer id, @EncodeParam EducationalDegree record) {
        record.setEducationalDegreeId(id);
        int update = context.getEducationalDegreeMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<EducationalDegree> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<EducationalDegree> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(EducationalDegree::getEducationalDegreeId, id);
            return Optional.of(context.getEducationalDegreeMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }
}
